library(foreign)
library(MASS)
library(plyr)
library(lme4)
library(ordinal)
library(mvtnorm)
library(arm)

# import demographics
demos1990 <- read.dta("1990 Census.dta")
demos2000 <- read.dbf("Lags.dbf")
demos2000 <- demos2000[!duplicated(demos2000[,"ZCTA"]),]

# merge demographics
demos.m <- merge(demos1990, demos2000, by.x = "zip", by.y = "ZCTA", all.y = T)

# import data frame
#df <- read.csv("SCCBS2000.csv")

# merge census demos and dataframe
df.m <- merge(df, demos.m, "zip", all.x = T)

# create recode function (0 - 1 rescaling)
recode_01 <- function(x) {
  x <- as.numeric(as.character(x))
  (x - min(x, na.rm=T))/(max(x,na.rm=T) - min(x,na.rm=T))}

# create main independent variables
fb_d <- recode_01(df.m[,"pctfb00"] - df.m[,"pctforbornzip90"])
fb_lag <- recode_01(df.m[,"PCTFB00LAG"])
fb_i <- recode_01(df.m[,"pctfb00"])
pctfb <- recode_01(df.m[,"pctforbornzip90"])

# used for graphs (original scale)
fb_d.n <- (df.m[,"pctfb00"] - df.m[,"pctforbornzip90"])/100
fb_lag.n <- (df.m[,"PCTFB00LAG"])/100

hisp_d <- recode_01(df.m[,"pcthisp00"] - df.m[,"pcthispzip90"]) 
hisp_lag <- recode_01(df.m[,"PCTHS00LAG"]) 
hisp_i <- recode_01(df.m[,"pcthisp00"])
pcthisp <- recode_01(df.m[,"pcthispzip90"])

house_lag <- recode_01(df.m[,"PCTMH00LAG"])
unemp_lag <- recode_01(df.m[,"PCTUN00LAG"])


# create and recode individual-level controls
age <- recode_01(df.m[,"age"])
education <- recode_01(mapvalues(df.m[,"education"], 
                                 levels(df.m[,"education"]), c(NA,3,4,5,2,1,5,3)))
tenure <- recode_01(mapvalues(df.m[,"livcom"], 
                              levels(df.m[,"livcom"]), c(NA,6,4,1,5,2,3)))
ideology <- recode_01(mapvalues(df.m[,"ideo"], 
                                levels(df[,"ideo"]), c(NA,3,3,4,2,5,1)))
income <- recode_01(mapvalues(df.m[,"income"], 
                              levels(df[,"income"]), c(NA,6,1,3,4,5,2,2,3)))
gender <- as.numeric(df.m[,"gender"] == "male")
own <- as.numeric(df.m[,"own"] == "own")
white <- as.numeric(df.m[,"racwhite"] == "yes")
hispanic <- as.numeric(df.m[,"hispan"] == "yes")
black <- as.numeric(df.m[,"black"] == "black")
asian <- as.numeric(df.m[,"racasian"] == "yes")
citizen <- as.numeric(df.m[,"citizen"] == "citizen")
kids <- as.numeric(df.m[,"kids"])

# create and recode contextual controls
pctblack <- recode_01(df.m[,"pctblackzip90"])
pctasian <- recode_01(df.m[,"pctasianzip90"])
medhsvl <- recode_01(df.m[,"medspropvlzip90"])
pctunemp <- recode_01(df.m[,"pctunempzip90"])
urban <- recode_01(df.m[,"urbpct90"])
community <- df.m$communty

# code outcome variables
exit <- as.numeric(df.m[,"stay"] == "no")

immig <- as.numeric(paste(mapvalues(df.m[,"immig"], 
                                    levels(df.m[,"immig"]), 
                                    c(NA, 3, 4, 5, 2, 1))))

